package quanlyhochieu.taomoi;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.faces.model.SelectItem;

import dbutil.ConnectDB;
public class TruyXuatViTriTu {
	private SelectItem[] listItemViTriTu = null;

	public SelectItem[] getListItemViTriTu() {
		return this.listItemViTriLuuTru();
	}

	public void setListItemViTriTu(SelectItem[] listItemViTriTu) {
		this.listItemViTriTu = listItemViTriTu;
	}

	
	public boolean distincViTriTu(List<ViTriLuuTru> list, ViTriLuuTru viTriTu){
		boolean result = true;
		for (ViTriLuuTru l : list) {
			if((l.getViTriTu().trim()).equals(viTriTu.getViTriTu().trim())){
				result = false;
				break;
			}
			
		}
		return result;
	}
	// lay ra danh cac nhom chuc nang
	/**************************************
	 * Method name		: dsViTriLuuTru
	 * Return type		: List<ViTriLuuTru>
	 * Description		:
	 * Created date		: Aug 15, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public List<ViTriLuuTru> dsViTriLuuTru() {
		List<ViTriLuuTru> listViTriLuuTru = new ArrayList<ViTriLuuTru>();
		try{
		String SQL = "SELECT DISTINCT  id,vitritu FROM kholuutru";
		ArrayList array = ConnectDB.getDataTypeList(SQL);
		System.out.println("array "+array.size());
		System.out.println(SQL);
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();
			for (int i = 0; i < array.size(); i++) {
				ViTriLuuTru LV = new ViTriLuuTru();
				hash = (HashMap<String, Object>) array.get(i);
				LV.setId(Integer.parseInt(hash.get("id").toString()));
				LV.setViTriTu(hash.get("vitritu").toString());
				if (this.distincViTriTu(listViTriLuuTru, LV) == true){
					listViTriLuuTru.add(LV);
					System.out.println("Vi tri "+LV.getViTriTu());
				}
				}			
		}
		}
		catch(Exception ex){
			System.out.println("Loi");
		}		
		System.out.println("Vi tri tu "+listViTriLuuTru.size());
		return listViTriLuuTru;
	}

	// tao cac selectItem tuong ung
	/**************************************
	 * Method name		: listItemViTriLuuTru
	 * Return type		: SelectItem[]
	 * Description		:
	 * Created date		: Aug 15, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public SelectItem[] listItemViTriLuuTru() {
		List<ViTriLuuTru> listFunct = this.dsViTriLuuTru();
		SelectItem[] arrItem = new SelectItem[listFunct.size()];
		for (int i = 0; i < listFunct.size(); i++) {
			ViTriLuuTru FD = listFunct.get(i);
			arrItem[i] = new SelectItem(FD.getViTriTu(), FD.getViTriTu());
		}
		return arrItem;
	}
	
}
